using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System.Linq;

namespace ReactiveLang
{
    using Util;

    class SubjectBase<TParent>
    {
        LinkedList<TParent> _parents = new LinkedList<TParent>();

        protected IEnumerable<TParent> Parents
        {
            get { return _parents; }
        }

        public IDisposable Activate(TParent parent)
        {
            Contract.Requires<ArgumentNullException>(parent != null, "parent");
            var node = _parents.AddLast(parent);
            return Disposable.Create(() => node.List.Remove(node));
        }

        protected void ForEach(Action<TParent> action)
        {
            Contract.Requires(action != null);
            foreach(var p in _parents.ToList())
                action(p);
        }
    }
}